# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
 
class Cartasur::AnswersController < Cartasur::ApplicationController
  include Geokit::Geocoders

  def index
    @cuser = current_user
    @users = Cartasur::User.all #.collect{ |u| [u.login, u.id]}
    @protocols = []
    @answers = []
  end

  def new
   @answer = Cartasur::Answer.new
  end


  def show
    @answer = Cartasur::Answer.find(params[:id])
  end

  def create
    answer_hash = params[:cartasur_answer]
    unless answer_hash.nil?
        #set protocol id
         @answer = Cartasur::Answer.new(answer_hash)
         if @answer.save
           flash[:notice] = "Successfully saved!"
           redirect_to @visited
         else
           flash[:alert] = "There was a problem when saving"
           render :action => 'new'
         end
    end

  end

  def edit
    @answer  = Cartasur::Answer.find(params[:id])

  end
  def update
    @visited = Cartasur::Answer.find(params[:id])

    if @visited.update_attributes(params[:cartasur_answer])
      flash[:notice] = "The item was successfully updated!"
      redirect_to @answer
    else
      flash[:alert] = "There was a problem when updating"
    end
  end

  # ---------------- selected ajax requests -----------------------
  def ajaxuseranswers

    #return unless secureajaxrequest

    pid = params[:protocol_id]

    unless pid.nil?

      protocols = Cartasur::Answer.where(" protocol_id = ?", pid)

    end
    respond_to do |format|
      format.json  { render json: protocols }
    end
  end

  def ajaxuserobjectives

    #return unless secureajaxrequest

      @user = Cartasur::User.find(params[:user_id])

      unless @user.nil?

        protocols = Cartasur::Protocol.where(" users_id = ?", @user.id)
        #filter if search term is given
        if (params[:term])
          protocols.select!{ |p| p.objective_text.downcase.include? params[:term].downcase }
        end
      end


      respond_to do |format|
        format.json  { render json: protocols, methods: [:objective_text] }
      end
  end


  def ajaxcartasurtrackhtml
    track_id = params[:track]
    track_id = track_id.gsub(/track-/,"").to_i  unless track_id.nil?

    begin
    track_tags = Cartasur::Track.find(track_id).popular_track_tags.all

    links = []

    links = generate_tag_cloud(track_tags)  unless track_tags.blank?
    links = links.join(",")  unless links.blank?
    rescue Exception => exception
      logger.info("Cartasur: Ajax request for track list html failed for track #{track_id}")
      links = "no data found"
    end
    render text: links, status: "200 OK"
  end


  # secure request. call this in all ajax methods
  def secureajaxrequest
    if current_user.nil?
      render :json=> {msg: "not authenticated"}
      false
    else
      true
    end
  end
  # -------

  # copied form TagsHelper, modified for this case
  def generate_tag_cloud(tags_clouds,tags_param="", category="user")   #category "tags"?

    links =  []
    unless tags_clouds.blank?
      #create the tag-cloud
      tag_cloud(tags_clouds, %w(size0 size1 size2 size3 size4)).map { |tag, css_class|
        url=tags_param.clone
        url= url << ("#{category}:#{tag.tag_name}") << ","
        link= view_context.link_to("#{tag.tag_name}", {:action => :filter, :controller => :tags, :tags => url}, :class => css_class, :title => "#{tag.count} measures")
        links << link
      }
    else
      #yield("no tag")
    end
    links
  end

  #helper to zip
  def tag_cloud(tags, s1)
    # zip em
    tags.blank?  ? [] :  tags.zip(s1)
  end

end
